package com.wnhz.work.jy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wnhz.work.jy.R;
import com.wnhz.work.jy.bean.ShoppingBean;

import org.xutils.x;

import java.util.List;

/**
 * Created by Administrator on 2016/9/5 0005.
 */
public class ShoppingAdapter extends BaseAdapter {
    private Context context;
    private List<ShoppingBean> shopList;
    private View.OnClickListener listener;
    public boolean isEdit;

    private int type;//type == 1 二维码订单信息

    public boolean isEdit() {
        return isEdit;
    }

    public void setEdit(boolean isEdit) {
        this.isEdit = isEdit;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public ShoppingAdapter(Context context, List<ShoppingBean> shopList, View.OnClickListener listener) {
        this.context = context;
        this.shopList = shopList;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return shopList.size();
    }

    @Override
    public Object getItem(int position) {
        return shopList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ShopViewHolder viewHolder = new ShopViewHolder();
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.shopping_item, null);

            viewHolder.rl_selector = (RelativeLayout) convertView.findViewById(R.id.rl_selector);//选中外围布局

            viewHolder.rl_num = (RelativeLayout) convertView.findViewById(R.id.rl_num);//控制数量
            viewHolder.tv_delete = (ImageView) convertView.findViewById(R.id.tv_delete);//删除
            viewHolder.tv_minus = (ImageView) convertView.findViewById(R.id.tv_minus);//减少
            viewHolder.tv_add = (ImageView) convertView.findViewById(R.id.tv_add);//增加
            viewHolder.tv_shopping_number = (TextView) convertView.findViewById(R.id.tv_shopping_number);//编辑数量

            viewHolder.iv_selector = (ImageView) convertView.findViewById(R.id.iv_selector);//选中
            viewHolder.iv_pic = (ImageView) convertView.findViewById(R.id.iv_pic);//图片
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);//名称
            //viewHolder.tv_color = (TextView) convertView.findViewById(R.id.tv_color);//介绍内容
            viewHolder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);//价格
            viewHolder.tv_numbers = (TextView) convertView.findViewById(R.id.tv_numbers);//数量
            viewHolder.tv_isEnough = (TextView) convertView.findViewById(R.id.tv_isEnough);//库存是否剩余

            viewHolder.lacking = convertView.findViewById(R.id.lacking);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ShopViewHolder) convertView.getTag();
        }
        x.image().bind(viewHolder.iv_pic, shopList.get(position).getGoodImg());
        viewHolder.tv_title.setText(shopList.get(position).getGoodName());
        viewHolder.tv_money.setText("¥" + shopList.get(position).getGoodPrice());

        if (shopList.get(position).isSelect() == true) {
            viewHolder.iv_selector.setImageResource(R.mipmap.ic_xuanze31_li);
        } else {
            viewHolder.iv_selector.setImageResource(R.mipmap.ic_xuanze_nor);
        }

        if ("1".equals(shopList.get(position).getIsEnough())) {
            viewHolder.tv_isEnough.setVisibility(View.GONE);
            viewHolder.lacking.setVisibility(View.GONE);
        } else {
            viewHolder.tv_isEnough.setVisibility(View.VISIBLE);
            viewHolder.lacking.setVisibility(View.VISIBLE);
            viewHolder.tv_isEnough.setText("库存不足");
        }

        if ("1".equals(shopList.get(position).getGoodNumber())){
            viewHolder.tv_minus.setImageResource(R.mipmap.ic_decase_nor);
        }else {
            viewHolder.tv_minus.setImageResource(R.mipmap.ic_decase);
        }


        if (isEdit) {
            viewHolder.rl_num.setVisibility(View.VISIBLE);
            viewHolder.tv_delete.setVisibility(View.VISIBLE);
            viewHolder.tv_money.setVisibility(View.INVISIBLE);
            viewHolder.tv_numbers.setVisibility(View.INVISIBLE);
            viewHolder.rl_selector.setVisibility(View.GONE);
            //convertView.findViewById(R.id.tv_color).setVisibility(View.INVISIBLE);
        } else {
            viewHolder.rl_num.setVisibility(View.INVISIBLE);
            viewHolder.tv_delete.setVisibility(View.INVISIBLE);
            viewHolder.tv_money.setVisibility(View.VISIBLE);
            viewHolder.tv_numbers.setVisibility(View.VISIBLE);
            //viewHolder.rl_selector.setVisibility(View.VISIBLE);
            if (type == 1) {
                viewHolder.rl_selector.setVisibility(View.GONE);
            } else {
                viewHolder.rl_selector.setVisibility(View.VISIBLE);
            }
            //convertView.findViewById(R.id.tv_color).setVisibility(View.VISIBLE);
        }

        String getNum = shopList.get(position).getGoodNumber();
//        int num = Integer.valueOf(getNum);

        viewHolder.tv_shopping_number.setText(getNum);
        viewHolder.tv_numbers.setText("x" + getNum);

        viewHolder.rl_selector.setOnClickListener(listener);
        viewHolder.rl_selector.setTag(position);
        viewHolder.tv_delete.setOnClickListener(listener);
        viewHolder.tv_delete.setTag(position);
        viewHolder.tv_minus.setOnClickListener(listener);
        viewHolder.tv_minus.setTag(position);
        viewHolder.tv_add.setOnClickListener(listener);
        viewHolder.tv_add.setTag(position);

        return convertView;
    }

    class ShopViewHolder {
        RelativeLayout rl_num;
        RelativeLayout rl_selector;
        ImageView iv_selector;
        ImageView iv_pic;
        ImageView tv_delete;
        TextView tv_title;
        TextView tv_money;
        TextView tv_numbers;
        ImageView tv_minus;
        ImageView tv_add;
        TextView tv_shopping_number;
        TextView tv_isEnough;
        View lacking;
    }
}
